<?php

class ZCrop extends CActiveRecord
{
    /**
     *
     * //1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 
     * 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF 
     * 
     * @param type $src_img
     * @param type $tar_img
     * @param type $dst_w
     * @param type $dst_h
     * @param type $isSharp
     * @return type 
     */
    static public function thumbcrop($src_img, $tar_img, $dst_w, $dst_h, $isSharp=false,$quality=80) {
        $picInfo = @getimagesize($src_img);  // 获取原图尺寸
        
        if(!$picInfo) return null;
        
        $src_w=$picInfo[0];
        $src_h=$picInfo[1];
        $mine=$picInfo[2];
        
        if (empty($src_w) || empty($src_h)) {
            return false;
        }

        $sizeInfo = self::getSize($src_w, $src_h, $dst_w, $dst_h);
        


        $source = self::loadJpeg($src_img,$mine);
        if (!$source) return false;
       
        if($dst_w!=0 || $dst_h!=0){
            $image_p = imagecreatetruecolor($sizeInfo['w'], $sizeInfo['h']);

            imagecopy($image_p, $source, 0, 0, $sizeInfo['x'], $sizeInfo['y'], $src_w, $src_h);

            $scale = $sizeInfo['dst_w'] / $sizeInfo['w'];
            $target = imagecreatetruecolor($sizeInfo['dst_w'], $sizeInfo['dst_h']);

            $final_w = intval($sizeInfo['w'] * $scale);
            $final_h = intval($sizeInfo['h'] * $scale);

            imagecopyresampled($target, $image_p, 0, 0, 0, 0, $final_w+1, $final_h+1, $sizeInfo['w'], $sizeInfo['h']);
            imageantialias($target, true);
        }else{
            $target=$source;
        }

        
        if($isSharp){
            $spnMatrix = array(array(0, -1, -1),
                array(-1, 14, -1),
                array(-1, -1, 0));
            $divisor = 8;
            $offset = 0;
            imageconvolution($target, $spnMatrix, $divisor, $offset);  
        }

        $result=imagejpeg($target, $tar_img, $quality);
        imagedestroy($target); 
        return $result;
      
    }
    
    static public function loadJpeg($imgname, $mine) {
        if($mine == 1) {
            $im = @imagecreatefromgif($imgname);
        }elseif ($mine == 2) {
            $im = @imagecreatefromjpeg($imgname);
        }elseif ($mine == 3) {
            $im = @imagecreatefrompng($imgname);
        }elseif ($mine == 6) {
            $im = @imagecreatefromwbmp($imgname);
        }else{
            return null;
        }

        if (!$im) { /* See if it failed */
            $im = imagecreatetruecolor(150, 30); /* Create a blank image */
            $bgc = imagecolorallocate($im, 255, 255, 255);
            $tc = imagecolorallocate($im, 0, 0, 0);
            imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
            /* Output an errmsg */
            imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
        }
        return $im;
    }
    
    static function getSize($src_w, $src_h, $dst_w, $dst_h) {
        if (empty($src_w) || empty($src_h)) {
            return false;
        }

        if ($dst_w == 0 && $dst_h != 0) {
            $src_scale = $dst_scale = $dst_w / $dst_h;
        } elseif ($dst_h == 0 && $dst_w != 0) {
            $src_scale = $dst_scale = $dst_h / $dst_w;
        } elseif ($dst_h == 0 && $dst_w == 0) {
            $src_scale = $dst_scale = 1;
        } else {
            $dst_scale = $dst_h / $dst_w; //目标图像长宽比
            $src_scale = $src_h / $src_w; // 原图长宽比			
        }

        if ($src_scale > $dst_scale) {  // 过高
            $w = intval($src_w);
            $h = intval($dst_scale * $w);
            $x = 0;
            $y = ($src_h - $h) / 3;
        } elseif ($src_scale == $dst_scale) {  // 等比例缩放
            $w = $src_w;
            $h = $src_h;
            if ($dst_w == 0 && $dst_h != 0) {
                $dst_w = $src_w * ($dst_h / $src_h);
            } elseif ($dst_h == 0 && $dst_w != 0) {
                $dst_h = $src_h * ($dst_w / $src_w);
            } elseif ($dst_h == 0 && $dst_w == 0) {
                $dst_w = $src_w;
                $dst_h = $src_h;
            }

            $x = 0;
            $y = 0;
        } else { // 过宽
            $h = intval($src_h);
            $w = intval($h / $dst_scale);
            $x = ($src_w - $w) / 2;
            $y = 0;
        }

        $data['w'] = $w;
        $data['h'] = $h;
        $data['x'] = $x;
        $data['y'] = $y;
        $data['dst_w'] = (int) $dst_w;
        $data['dst_h'] = (int) $dst_h;
        return $data;
    }

}